/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.just.dusin.ooii.component.standard;

import java.util.Properties;
import java.util.logging.Level;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;

import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.log4j.Logger;
import org.just.dusin.ooii.component.Source;
import org.dusin.webcoal.exception.OOIIException;

/**
 *
 * @author Administrator
 */
public class AMQWithResponseSource extends Source {

    private String brokerName="broke";
    

    public String getBrokerName() {
        return brokerName;
    }

    public void setBrokerName(String brokerName) {
        this.brokerName = brokerName;
    }

    public String getBrokerURL() {
        return brokerURL;
    }

    public void setBrokerURL(String brokerURL) {
        this.brokerURL = brokerURL;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getQueueName() {
        return queueName;
    }

    public void setQueueName(String queueName) {
        this.queueName = queueName;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }
    private String brokerURL = "tcp://localhost:61616";
    private String queueName="coal";    
    private int inteval=100;

    public void setInteval(int inteval) {
        this.inteval = inteval;
    }
    
    private String user;
    private String password;
    private static Logger logger = Logger.getLogger(AMQSink.class.getName());
    private MessageProducer responseProducer;
    private MessageConsumer messageConsumer;
    Session session;
    
    
    @Override
    public void initComponent(Properties props) {
    }

    @Override
    public void destroyComponent() throws Exception {
    }

    @Override
    public void run() {
        ConnectionFactory factory = new ActiveMQConnectionFactory(brokerURL);
        int i=0;
        while (true) {
            try {
                
                Connection connection = factory.createConnection(user, password);
                connection.start();
                session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                Destination destination = session.createQueue(queueName);
                messageConsumer = session.createConsumer(destination);
                TextMessage txtMessage = (TextMessage)messageConsumer.receive(1000);
                responseProducer = session.createProducer(null);
                if(txtMessage!=null)    {
                    
                    sink.process(txtMessage.getText());
                    //发送确认信息
                    logger.info("发送确认消息");
                    String id = txtMessage.getJMSMessageID();
                    TextMessage response = this.session.createTextMessage(id);
                    responseProducer.send(txtMessage.getJMSReplyTo(), response);
                }
                if (messageConsumer != null) {
                    messageConsumer.close();
                }
                if (session != null) {
                    session.close();
                }
                if (connection != null) {
                    connection.close();
                }
                
                Thread.sleep(inteval);
            } catch (JMSException e) {
                logger.error(e.getMessage());
            }catch(OOIIException e){
                logger.error(e.getMessage());
            }catch(InterruptedException e){
                logger.error(e.getMessage());
            }finally{
                try {
                    Thread.sleep(inteval);
                } catch (InterruptedException ex) {
                    java.util.logging.Logger.getLogger(AMQWithResponseSource.class.getName()).log(Level.SEVERE, null, ex);
                }
            }            
        }
    }
}
